import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken , getID } from '../utils/localData.js'

Vue.use(VueRouter);

import Login from '../views/login.vue'
import Home from '../views/home.vue'
import ArticleList from '../views/homePage/articlelist.vue'
import ArticlePost from '../views/homePage/articlepost.vue'
import CategoryList from '../views/homePage/categorylist.vue'
const routes = [
	{path: '/' , redirect: '/login'},
	{path: '/login' , component: Login},
	{	path: '/home' , 
		component: Home,
		children: [
			{path: 'articlelist' , component: ArticleList},
			{path: 'articlepost/:id?' , component: ArticlePost},
			{path: 'category' , component: CategoryList}
		]
	 
	 },
]

const router = new VueRouter({
	routes,
	mode: 'history'
})

router.beforeEach((to , from , next) => {
	if(to.path === '/login'){
		next();
	}else{
		if(getToken() && getID()){
			next();
		}else{
			next({path: '/login'});
		}
	}
})

export default router